#include "OLStatusbar.h"
#include "OLButton.h"
#include "OLPixmapWidget.h"
#include "OLAppTemplateDefs.h"
#include "OLApplication.h"
#include "OLInfoButton.h"

#include <QtGui/QPainter>
#include <QtGui/QLinearGradient>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>

OLStatusbar::OLStatusbar(QWidget *parent) :
    QWidget(parent)
{
    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setMargin(1);
    layout->setSpacing(1);

    OLButton* homeButton = new OLButton(this);
    homeButton->setFixedSize(FINGER_SIZE, FINGER_SIZE);
    homeButton->opaque();
    homeButton->iconButton(ButtonIcons(QPixmap(":/icons/home_off"), QPixmap(":/icons/home_on")));

    OLButton* title = new OLButton(this);
    title->setObjectName("title");
    title->setFixedHeight(FINGER_SIZE);
    title->opaque();
    title->textIconButton("", ButtonIcons(QPixmap(":/icons/menu_off"), QPixmap(":/icons/menu_on")), Qt::AlignCenter);
    title->setTextStyleSheet("font-family: \"Helvetica 55 Light\"; ; font-size: 24px");
    title->disable();
    title->hideIcon();

    OLButton* closeBtn = new OLButton(this);
    closeBtn->setFixedSize(FINGER_SIZE, FINGER_SIZE);
    closeBtn->opaque();
    closeBtn->iconButton(ButtonIcons(QPixmap(":/icons/close_off"), QPixmap(":/icons/close_on")));

    OLButton* returnBtn = new OLButton(this);
    returnBtn->setFixedSize(FINGER_SIZE, FINGER_SIZE);
    returnBtn->opaque();
    returnBtn->iconButton(ButtonIcons(QPixmap(":/icons/back_off"), QPixmap(":/icons/back_on")));
    returnBtn->hide();

    layout->addWidget(homeButton);
    layout->addWidget(title);
    layout->addWidget(closeBtn);
    layout->addWidget(returnBtn);
    setLayout(layout);

    connect(homeButton, SIGNAL(clicked()), qApp, SLOT(homeButtonClicked()));
    connect(closeBtn, SIGNAL(clicked()), qApp, SLOT(quit()));
    connect(returnBtn, SIGNAL(clicked()), qApp, SLOT(removeStackedView()));
    connect(qApp, SIGNAL(titleChanged(QString)), title, SLOT(setText(QString)));

    connect(qApp, SIGNAL(viewAdded(QWidget*)), closeBtn, SLOT(hide()));
    connect(qApp, SIGNAL(viewAdded(QWidget*)), returnBtn, SLOT(show()));
    connect(qApp, SIGNAL(baseViewActivated()), returnBtn, SLOT(hide()));
    connect(qApp, SIGNAL(baseViewActivated()), closeBtn, SLOT(show()));

    connect(this, SIGNAL(menuEnabled()), title, SLOT(enable()));
    connect(this, SIGNAL(menuEnabled()), title, SLOT(showIcon()));
    connect(this, SIGNAL(menuDisabled()), title, SLOT(disable()));
    connect(this, SIGNAL(menuDisabled()), title, SLOT(hideIcon()));

    connect(title, SIGNAL(clicked()), qApp, SLOT(showMenu()));

}

void OLStatusbar::paintEvent(QPaintEvent*)
{
//    QPainter p(this);

//    p.setPen(Qt::NoPen);
//    p.setBrush(OLStyles::toolbarBackgroundBrush(rect()));
//    p.drawRect(rect());

//    p.setPen(OLStyles::buttonSeparator());
//    p.drawLine(0, height()-1, width(), height()-1);

//    // draw separators between widgets
//    QList<OLButton*> widgets = findChildren<OLButton*>();
//    int childCount = widgets.count();
//    for(int ii=0; ii<childCount-1; ++ii) {
//        OLButton* btn = widgets[ii];
//        if( btn->isVisible() && btn->parentWidget()==this ) {
//        int xpos = widgets[ii]->geometry().right()+1;
//        p.drawLine(xpos, 0, xpos, height());
//        }
//    }
}

void OLStatusbar::handleMenuState(OLMenuSurface::OLMenuState menustate)
{
    switch(menustate)
    {
    case OLMenuSurface::NoMenu:
        {
        emit menuDisabled();
        } break;
    case OLMenuSurface::MenuAvailable:
        {
        emit menuEnabled();
        } break;
    default:
        break;
    }
}
